//
//  ViewController.m
//  update
//
//  Created by student on 16/3/30.
//  Copyright © 2016年 wk. All rights reserved.
//

#import "ViewController.h"

#define kBoundary @"wu123456"
#define kNewLine @"\r\n"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self uploadFile];
}

//-------请求头
//POST /MJServer/upload HTTP/1.1
//Host: 192.168.13.108:8080
//User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:33.0) Gecko/20100101 Firefox/33.0
//Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
//Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
//Accept-Encoding: gzip, deflate
//Referer: http://192.168.13.108:8080/MJServer/
//Connection: keep-alive
//Content-Type: multipart/form-data; boundary=---------------------------7398234522824416341057682768
//Content-Length: 472
//


//--------上传数据需要构建请求体内容如下


//                                                                -----------------------------7398234522824416341057682768
// Content-Disposition: form-data; name="file"; filename="小散选股问题点.txt"
// Content-Type: text/plain

//  文件二进制数据

//  -----------------------------7398234522824416341057682768
//Content-Disposition: form-data; name="username"
//                                                                
//abc
//                                                                -----------------------------7398234522824416341057682768--
//

//内容说明:
//第一部分:文件参数
//--分隔字符串
//Content-Disposition: form-data; name="file"; filename="文件名.txt" //文件名
// Content-Type: text/plain      // 文件类型 // Content-Type参考: http://tool.oschina.net/commons/

//第二部分:上传的文件数据
//数据
//第三部分:其他参数
//--分隔符字符串
//Content-Disposition: form-data; name="username"
//
//abc
//第四部分:结尾
//--分隔字符串
- (void)uploadFile{
    //请求的网址
    NSURL *url = [NSURL URLWithString:@"http://192.168.13.108:8080/MJServer/upload"];
    //请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求头
    NSString *headerStr = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",kBoundary];
    [request setValue:headerStr forHTTPHeaderField:@"Content-Type"];
    
    //------------------------------------------------------------------------------
      //构建请求体内容mData
    NSMutableData *mData = [NSMutableData data];
    NSMutableString *mStr = [NSMutableString string];
    //1. 文件参数
    [mStr appendString:kNewLine];
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:kNewLine];
    [mStr appendString:@"Content-Disposition: form-data; name=\"file\"; filename=\"小散选股问题点.txt\""];
    [mStr appendString:kNewLine];
    [mStr appendString:@"Content-Type: text/plain"];
    [mStr appendString:kNewLine];
    [mStr appendString:kNewLine];
    
    [mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];
    //2. 数据
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"小散选股问题点.txt" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    [mData appendData:data];
    //3. 其他参数
    mStr = [NSMutableString string];
    [mStr appendString:kNewLine];
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:kNewLine];
    [mStr appendString:@"Content-Disposition: form-data; name=\"username\""];
    [mStr appendString:kNewLine];
    [mStr appendString:kNewLine];
    [mStr appendString:@"abc"];
    [mStr appendString:kNewLine];
    //4. 结尾
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];
    
    request.HTTPBody = mData;
    
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
                               if (connectionError == nil)
                               {
                                   NSError *error;
                                   NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
                                                                                        options:NSJSONReadingMutableLeaves
                                                                                          error:&error];
                                   NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   if (error == nil) {
                                       NSLog(@"%@",dict);
                                   }
                                   else{
                                       NSLog(@"%@",[error localizedDescription]);
                                   }
                               }
                               else{
                                   NSLog(@"%@",[connectionError localizedDescription]);
                               }
                           }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end











